import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
/*
1:将路由分离
   1:需要权限处理的路由
   2:不需要权限处理的路由.比如:登陆页,404,首页
   a:默认只能访问静态路由(不需要权限处理的路由)
   需要将路由分二大块.
   1:静态路由(不需要权限处理的路由)
   2:动态路由(需要权限处理的路由)
              默认只使用静态路由
      a:在router/modules/模块名.js
         js单独暴露出路由配制   
      b:将分离出去的所有路由配制再导入成一个单独数组用于后期匹配使用 
     
 2:等到登陆完成获取用户信息后,再通过路由的addRoutes添加相应路由
   将路由的所有配制交给vuex进行管理
     a:store/modules/routes.js
     b:加入到主模块的modules下
     c:通过vuex管理所有的路由配制
*/
import approvals from './modules/approvals'
import attendances from './modules/attendances'
import departments from './modules/departments'
import employees from './modules/employees'
import permission from './modules/permission'
import salarys from './modules/salarys'
import settings from './modules/settings'
import social from './modules/social'
// 动态路由
export const asyncRoutes = [
  approvals,
  attendances,
  departments,
  employees,
  permission,
  salarys,
  settings,
  social
]
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 不显示在菜单上
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 如果希望路由配制项显示在菜单上,那它要么没有children要么children只有一个项(其它项的hidden:true,hidden没有配制或者为flase只有一个项)
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'dashboard', icon: 'dashboard' }
      }
    ]
  }

  // 404 page must be placed at the end !!!
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: [...constantRoutes]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
